package net.wanho.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import net.wanho.client.UserClient;
import net.wanho.entity.User;
import net.wanho.util.BlockExceptionUtil;
import net.wanho.vo.AjaxResult;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Author：汤小洋
 * Date：2024-09-10 10:53
 * Description：<描述>
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    // private RestTemplate restTemplate;
    private UserClient userClient;

    @SentinelResource(value = "UserTestResource", blockHandlerClass = BlockExceptionUtil.class, blockHandler = "blockHandler")
    @GetMapping("/test")
    public AjaxResult test(){
        // String str = restTemplate.getForObject("http://localhost:6001/users/test", String.class);
        System.out.println(userClient);
        String str = userClient.test();
        return AjaxResult.success(str);
    }

    @GetMapping
    public AjaxResult list(String username){
        return userClient.list(username);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Integer id){
        return userClient.removeById(id);
    }

    @PostMapping
    public AjaxResult save(@RequestBody User user){
        return userClient.save(user);
    }

    @PutMapping
    public AjaxResult update(@RequestBody User user){
        return userClient.update(user);
    }

    @GetMapping("/query")
    public AjaxResult query(@RequestParam Map<String,Object> map){
        return userClient.query(map);
    }

}
